<template>
  <div class="app-container">
    
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="100px">
      <el-form-item label="账号名" prop="accountName">
        <el-input
          v-model="queryParams.accountName"
          placeholder="请输入账号名"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="昵称" prop="nick">
        <el-input
          v-model="queryParams.nick"
          placeholder="请输入昵称"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="catUserAccountList" >
      <!-- <el-table-column label="id" align="center" prop="id" /> -->
      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="用户账号" align="center" prop="accountName" />
      <el-table-column label="用户昵称" align="center" prop="nick" />
      <el-table-column label="用户等级" align="center" prop="userLevel" />
      <el-table-column label="经验值" align="center" prop="exp" />
      <el-table-column label="可用荧光棒" align="center" prop="lightStick" />
      <!-- <el-table-column label="累计获得荧光棒" align="center" prop="balance" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="showExp(scope.row)"
          >经验值明细</el-button>
          <el-button
            size="mini"
            type="text"
            @click="showStack(scope.row)"
          >荧光棒明细</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleUpdateNumbers(scope.row.id)"
          >修改数值</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog title="修改经验值或荧光棒" :visible.sync="open" width="40%" >
      <el-form ref="elForm" :model="formData" :rules="rules" size="mini" label-width="100px">
        <el-form-item label="调整经验值" prop="exp">
          <el-input-number v-model="formData.exp" placeholder="点" controls-position=right>
          </el-input-number>
        </el-form-item>
        <el-form-item label="调整荧光棒" prop="stick">
          <el-input-number v-model="formData.stick" placeholder="个" controls-position=right>
          </el-input-number>
        </el-form-item>
        <el-form-item label="备注信息" prop="remarks">
          <el-input v-model="formData.remarks" type="textarea" :autosize="{minRows: 4, maxRows: 3}"
            :style="{width: '100%'}"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="onClose">取消</el-button>
        <el-button type="primary" @click="submitNumber">确定</el-button>
      </div>
    </el-dialog>
    
    <userExpLog v-if="expLogVisible" ref="expLog" />
    <userStackLog v-if="stackLogVisible" ref="stackLog" />
  </div>
</template>

<script>
import userExpLog from "./userExpLog"
import userStackLog from "./userStackLog"

import { listCatUserAccount, getCatUserAccount, updateByNumbers} from "@/api/system/catUserAccount";
export default {
  components:{
    userExpLog,
    userStackLog
  },
  data() {
    return {
      stackLogVisible:false,
      expLogVisible:false,

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 用户注册的账号表格数据
      catUserAccountList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        accountName: undefined,
        nick: undefined,
        userId: undefined
      },
      // 表单参数
      form: {},


      formData: {
        userId:undefined,
        exp: undefined,
        stick: undefined,
      },
      rules: {
        remarks: [{
          required: true,
          message: '请填写备注信息',
          trigger: 'blur'
        }],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户注册的账号列表 */
    getList() {
      this.loading = true;
      listCatUserAccount(this.queryParams).then(response => {
        this.catUserAccountList = response.rows;
        this.total =  parseInt(response.total) ;
        this.loading = false;
      });
    },
    showExp(row){
      this.expLogVisible = true;
      this.$nextTick(() => {
        this.$refs.expLog.init(row)
      })
    },
    showStack(row){
      this.stackLogVisible = true;
      this.$nextTick(() => {
        this.$refs.stackLog.init(row)
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 修改数值 */
    handleUpdateNumbers(id) {
      this.open = true;
      this.formData.userId = id
    },
    submitNumber() {
      this.$refs['elForm'].validate(valid => {
        if (valid) {
          updateByNumbers(this.formData).then(response => {
            if (response.code === 200) {
              this.msgSuccess("修改数值成功");
              this.open = false;
              this.getList()
            }
          });
        }
      })
    },
    onClose() {
      this.formData = {
        userId:undefined,
        exp: undefined,
        stick: undefined,
      }
      this.$refs['elForm'].resetFields()
    },
  }
};
</script>